
#include <QFont>
#include <QPainter>
#include <QPaintEvent>
#include <QPoint>
#include <QRectF>
#include <QResizeEvent>
#include <QSize>
#include <QVBoxLayout>

#include "core/engine.h"
#include "core/game.h"
#include "ui/button.h"
#include "ui/score.h"

Score::Score(QWidget* pkParent)
    : QWidget(pkParent),
      m_ratio(0.75),
      m_kBackground(":/image/board/board.png"),
      m_pkOkButton(new Button(":/image/button/normal_ok.png", this))
{
    // Check.
    Q_CHECK_PTR(m_pkOkButton);
    Q_ASSERT(!m_kBackground.isNull());

    // Setup widget.
    setAutoFillBackground(false);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    setLayout(new QVBoxLayout(this));
    layout()->setEnabled(false);
    layout()->addWidget(m_pkOkButton);

    // Connections.
    connect(m_pkOkButton, SIGNAL(clicked()), this, SIGNAL(home()));
}

Score::~Score()
{

}

void Score::paintEvent(QPaintEvent* pkEvent)
{
    // Propagate.
    QWidget::paintEvent(pkEvent);

    // Base scaling on background pixmap.
    qreal limitingRatio = qMin(width() * m_ratio / m_kBackground.width(),
                               height() * m_ratio / m_kBackground.height());

    // Paint.
    QPainter kPainter(this);

    // Paint the background.
    QSize kScaledBackgroundSize(m_kBackground.width() * limitingRatio,
                                m_kBackground.height() * limitingRatio);
    QPoint kBackgroundOffset((width() - kScaledBackgroundSize.width()) / 2,
                             (height() - kScaledBackgroundSize.height()) / 2 - kScaledBackgroundSize.height() / 8);

    kPainter.drawPixmap(QRectF(kBackgroundOffset.x(), kBackgroundOffset.y(),
                               kScaledBackgroundSize.width(), kScaledBackgroundSize.height()),
                        m_kBackground,
                        m_kBackground.rect());

    // Paint the text.
    kPainter.setFont(QFont("Wortellina", kScaledBackgroundSize.height() / 20));
    qreal margin = 0.2;

    kPainter.drawText(kBackgroundOffset.x() + kScaledBackgroundSize.width() * margin,
                      kBackgroundOffset.y() + kScaledBackgroundSize.height() * margin,
                      kScaledBackgroundSize.width() - (kScaledBackgroundSize.width() * margin) * 2,
                      kScaledBackgroundSize.height() - (kScaledBackgroundSize.height() * margin) * 2,
                      Qt::TextWordWrap,
                      trUtf8("Cows : %1\nTime : %2\nShots : %3").arg(Engine::instance().game().cowCount()).arg(Engine::instance().game().elapsed()).arg(Engine::instance().game().shots()));

    kPainter.drawText(kBackgroundOffset.x() + kScaledBackgroundSize.width() * margin,
                      kBackgroundOffset.y() + kScaledBackgroundSize.height() * margin,
                      kScaledBackgroundSize.width() - (kScaledBackgroundSize.width() * margin) * 2,
                      kScaledBackgroundSize.height() - (kScaledBackgroundSize.height() * margin) * 2,
                      Qt::AlignVCenter | Qt::AlignHCenter |  Qt::TextWordWrap,
                      trUtf8("Score : %4").arg(Engine::instance().game().score()));
}

void Score::resizeEvent(QResizeEvent* pkEvent)
{
    // Propagate.
    QWidget::resizeEvent(pkEvent);

    // Compute scaled background coordinates.
    qreal limitingRatio = qMin(width() * m_ratio / m_kBackground.width(),
                               height() * m_ratio / m_kBackground.height());
    QSize kScaledSize(m_kBackground.width() * limitingRatio, m_kBackground.height() * limitingRatio);
    QPoint kOffset((width() - kScaledSize.width()) / 2, (height() - kScaledSize.height()) / 2 - kScaledSize.height() / 8);

    // Button replace.
    int iWidth = m_pkOkButton->sizeHint().width() * limitingRatio;
    int iHeight = m_pkOkButton->sizeHint().height() * limitingRatio;
    int iX = (width() - iWidth) / 2;
    int iY = kOffset.y() + kScaledSize.height() - iHeight / 2;
    m_pkOkButton->setGeometry(iX, iY, iWidth, iHeight);
}
